package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Warehouse;
import cn.stylefeng.guns.modular.api.mapper.WarehouseMapper;
import cn.stylefeng.guns.modular.api.model.params.WarehouseParam;
import cn.stylefeng.guns.modular.api.model.result.WarehouseResult;
import  cn.stylefeng.guns.modular.api.service.WarehouseService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 仓库 服务实现类
 * </p>
 *
 * @author wusonghe
 * @since 2019-08-31
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Override
    public void add(WarehouseParam param){
        Warehouse entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WarehouseParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(WarehouseParam param){
        Warehouse oldEntity = getOldEntity(param);
        Warehouse newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WarehouseResult findBySpec(WarehouseParam param){
        return null;
    }

    @Override
    public List<WarehouseResult> findListBySpec(WarehouseParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(WarehouseParam param){
        Page pageContext = getPageContext();
        if (pageContext.getSize()==0) {
            List datas =  this.baseMapper.customList(param);
            return LayuiPageFactory.createDataInfo(datas);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(WarehouseParam param){
        return param.getWarehouseId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Warehouse getOldEntity(WarehouseParam param) {
        return this.getById(getKey(param));
    }

    private Warehouse getEntity(WarehouseParam param) {
        Warehouse entity = new Warehouse();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
